package com.zheng.hospital.api;

import com.zheng.hospital.common.result.CommonResult;
import com.zheng.hospital.common.util.AuthContextHolder;
import com.zheng.hospital.model.user.Patient;
import com.zheng.hospital.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 就诊人员管理
 */
@RestController
@RequestMapping("/api/user/patient")
public class PatientApiController {
    @Autowired
    private PatientService patientService;

    /**
     * 获取就诊人列表
     * @param request
     * @return
     */
    @GetMapping("auth/findAll")
    public CommonResult findAll(HttpServletRequest request) {
        //获取当前登录id
        Long userId = AuthContextHolder.getUserId(request);
        List<Patient> allUserId = patientService.findAllUserId(userId);
        return CommonResult.ok(allUserId);
    }

    /**
     * 添加就诊人
     * @param patient
     * @param request
     * @return
     */
    @RequestMapping("/auth/save")
    public CommonResult setPatient(@RequestBody Patient patient, HttpServletRequest request) {
        //获取当前登录过的id
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        patientService.save(patient);
        return CommonResult.ok();
    }

    /**
     * 根据id获取就诊人信息
     * @param id
     * @return
     */
    @GetMapping("auth/get/{id}")
    public CommonResult getPatient(@PathVariable Long id) {
        Patient patientId = patientService.getPatientId(id);
        return CommonResult.ok(patientId);
    }

    /**
     * 修改就诊人
     * @param patient
     * @return
     */
    @PostMapping("auth/update")
    public CommonResult updatePatient(@RequestBody Patient patient) {
        patientService.updateById(patient);
        return CommonResult.ok();
    }

    //删除就诊人
    @DeleteMapping("auth/remove/{id}")
    public CommonResult removePatient(@PathVariable Long id) {
        patientService.removeById(id);
        return CommonResult.ok();
    }

    //根据就诊人id获取就诊人信息
    @GetMapping("inner/get/{id}")
    public Patient getPatientOrder(@PathVariable Long id) {
        Patient patient = patientService.getPatientId(id);
        return patient;
    }
}
